package ner;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Util {

	
	static Set<String> caseInsensitiveSubtraction(Set<String> from,Set<String> argWithLowered){
		
		
		String ne=null;
		Iterator<String> it=from.iterator();
		while(it.hasNext())	{
			ne=it.next();
			if(argWithLowered.contains(ne.toLowerCase())){
				System.out.println(ne);
				it.remove();
				
			}
			
		}
		return from;
		
	}
	
	static Set<String> caseInsensitiveIntersection(Set<String> firstSet,Set<String> secondSetWithLowered){
		
		Set<String> intersection=new HashSet<String>();
		for(String s:firstSet){
			
			if(secondSetWithLowered.contains(s.toLowerCase())){
				intersection.add(s);
					
			}
		}
		
		return intersection;
		
	}
	
	
	public static int getWordDistance(String arg1,String arg2){
		
		String w1=arg1.trim().toLowerCase();
		String w2=arg2.trim().toLowerCase();
			
		if(w1.equals(w2)){
			return 0;
		}
		int lengthOfShorter=0;
		if(w1.length()>w2.length())
			lengthOfShorter=w1.length();
		else
			lengthOfShorter=w2.length();
		
		
		
		if((w1.startsWith(w2.substring(0,w2.length()-1)) || w2.startsWith(w1.substring(0,w1.length()-1)))){
			if(Math.abs(w1.length()-w2.length())<(0.5*(double)lengthOfShorter) && Math.abs(w1.length()-w2.length())<=4){
				return 1;
			}
		}
		return 100;
		
	}
	/*
	public b int getGeoWordDistance(String arg1,String arg2){
		
		String w1=arg1.trim().toLowerCase();
		String w2=arg2.trim().toLowerCase();
			
		if(w1.equals(w2)){
			return 0;
		}
		int lengthOfShorter=0;
		if(w1.length()>w2.length())
			lengthOfShorter=w1.length();
		else
			lengthOfShorter=w2.length();
		
		
		
		if((w1.startsWith(w2.substring(0,w2.length()-1)) || w2.startsWith(w1.substring(0,w1.length()-1)))){
			if(Math.abs(w1.length()-w2.length())<(0.5*(double)lengthOfShorter) && Math.abs(w1.length()-w2.length())<=4){
				return 1;
			}
		}
		else 
			return 0;
		
	}
	*/
	public static <T> Map<T,Integer> addMapsWithOccurrences(Map<T,Integer> map1,Map<T,Integer> map2){
		
		Map<T,Integer> result=new HashMap<T,Integer>();
		
		for(Map.Entry<T, Integer> entry:map1.entrySet()){
			result.put(entry.getKey(), entry.getValue());
		}
		
		for(Map.Entry<T, Integer> entry:map2.entrySet()){
			
			if(result.containsKey(entry.getKey())){
				Integer valueInM1=result.get(entry.getKey());
				result.put(entry.getKey(),entry.getValue()+valueInM1);
				
			}
			else{
				result.put(entry.getKey(), entry.getValue());
			}
			
		}
		
		return result;
		
	}
	
	
	public static <T> T getMostFrequentFromMap(Map<T,Integer> mapWithFrequency){
		
		int max=0;
		T mostFrequentElement=null;
		for(Map.Entry<T, Integer> entry:mapWithFrequency.entrySet()){
			
			if(entry.getValue()>max){
				max=entry.getValue();
				mostFrequentElement=entry.getKey();
			}
			
		}
		
		return mostFrequentElement;
		
	}
	
	public static <E,T> void showMap(Map<T,E> map){
		
		for(Map.Entry<T, E> entry:map.entrySet()){
			
			System.out.println("key="+entry.getKey()+" value="+entry.getValue());
			
		}
		
		
	}
	
	
}
